
const SUBWAYCOLOR = new Map();
SUBWAYCOLOR.set('地铁1号线', '#00B140')
SUBWAYCOLOR.set('地铁1号线', '#00B140')
SUBWAYCOLOR.set('地铁2号线', '#B94700')
SUBWAYCOLOR.set('地铁3号线', '#00A9E0')
SUBWAYCOLOR.set('地铁4号线', '#DA291C')
SUBWAYCOLOR.set('地铁5号线', '#A05EB5')
SUBWAYCOLOR.set('地铁6号线', '#00C7B1')
SUBWAYCOLOR.set('地铁7号线', '#0033A0')
SUBWAYCOLOR.set('地铁8号线', '#E45DBF')
SUBWAYCOLOR.set('地铁9号线', '#7B6469')
SUBWAYCOLOR.set('地铁10号线', '#F8779E')
SUBWAYCOLOR.set('地铁11号线', '#672146')
SUBWAYCOLOR.set('地铁12号线', '#A192B2')
SUBWAYCOLOR.set('地铁13号线', '#DE7C00')
SUBWAYCOLOR.set('地铁14号线', '#F2C75C')
SUBWAYCOLOR.set('地铁15号线', '#84BD00')
SUBWAYCOLOR.set('地铁16号线', '#1E22AA')
SUBWAYCOLOR.set('地铁17号线', '#017D83')
SUBWAYCOLOR.set('地铁18号线', '#1E90FF')
SUBWAYCOLOR.set('地铁19号线', '#439F6C')
SUBWAYCOLOR.set('地铁20号线', '#88DBDF')
SUBWAYCOLOR.set('地铁21号线', '#8F6740')
SUBWAYCOLOR.set('地铁22号线', '#1B9062')
SUBWAYCOLOR.set('地铁23号线', '#A9624A')
SUBWAYCOLOR.set('地铁24号线', '#F44F19')
SUBWAYCOLOR.set('地铁25号线', '#4A6AE0')
SUBWAYCOLOR.set('地铁26号线', '#C82C8A')
SUBWAYCOLOR.set('地铁27号线', '#3E8065')
SUBWAYCOLOR.set('地铁28号线', '#8777E7')
SUBWAYCOLOR.set('地铁29号线', '#A23049')
SUBWAYCOLOR.set('地铁30号线', '#CB9B47')
SUBWAYCOLOR.set('地铁31号线', '#CD7FF2')
SUBWAYCOLOR.set('地铁32号线', '#980CA0')
SUBWAYCOLOR.set('地铁34号线', '#168773')

export interface PathProps {
	walking: number //步行距离
	duration: number //时间
	stationCount: number //站点
	distance: number //全程距离
	geton: string //上车点
	getoff: string //下车点
	lines: { name: string, type: string, color?: string }[] //起点到终点的线路数
	fare: number //费用
}


export class CreatePolyline {
	defaultArray: any[][] = []
	polylineResult: any[][] = []
	setp: any[] = []
	result: any[][] = []
	trafficType = ''
	pathLine: PathProps[] = []
	lineCount = 0
	constructor(trafficType: string) {
		this.trafficType = trafficType
	}
	filterData(steps: any[]) {
		const dataArr = steps
		console.log('dataArr', dataArr)
		dataArr.forEach((item: any) => {
			if (item.mode === 'WALKING') {
				this.defaultArray.push([item])
			} else if (item.mode === "DRIVING") {
				this.defaultArray.push([item])
			} else if (item.mode === "TRANSIT") {
				this.defaultArray.push([...item.lines])
			} else {
				this.defaultArray.push([...item.lines])
			}
		})
	}
	setPolylineExchange(arr: any[][], index: number) {
		for (var i = 0; i < arr[index].length; i++) {
			this.setp[index] = arr[index][i];
			if (index != arr.length - 1) {
				this.setPolylineExchange(arr, index + 1)
			} else {
				this.polylineResult.push(JSON.parse(JSON.stringify(this.setp)))
			}
		}

	}
	analysis() {
		console.log('this.polylineResult', this.polylineResult)
		this.polylineResult.forEach((item: any[], index: number) => {
			const arr: any[] = []
			const path: PathProps = {
				walking: 0, //步行距离
				duration: 0, //时间
				stationCount: 0, //站点
				distance: 0,//全程距离
				geton: '',
				getoff: '',
				lines: [],
				fare: 0,
			}

			item.forEach((polylines: any) => {
				var route = polylines
				var coors: any[] = [], pl: any[] = [];

				coors = route.polyline

				if (route.vehicle === 'RAIL') {
					console.log('route', route)
				}

				if (route.vehicle === 'RAIL') {
					//如果是火车路线，直接取上车点坐标和下车点坐标
					pl.push({
						latitude: route.geton.location.lat,
						longitude: route.geton.location.lng
					})
					pl.push({
						latitude: route.getoff.location.lat,
						longitude: route.getoff.location.lng
					})
				} else {
					//坐标解压（返回的点串坐标，通过前向差分进行压缩）
					var kr = 100000;
					for (var i = 2; i < coors.length; i++) {
						coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
					}
					//将解压后的坐标放入点串数组pl中
					for (var i = 0; i < coors.length; i += 2) {
						pl.push({ latitude: coors[i], longitude: coors[i + 1] })
					}
				}


				path.duration += Number(route.duration)

				if (this.trafficType === 'driving') {
					//驾驶
					arr.push({
						points: pl,
						color: '#4fbe99',
						width: 6,
						borderColor: '#3cb18a',
						borderWidth: 1
					})
					path.fare = route.taxi_fare.fare
					path.distance = route.distance

				} else if (this.trafficType === 'bus') {
					arr.push({
						points: pl,
						color: route.mode === 'WALKING' ? '#999' : '#4fbe99',
						width: 6,
						borderColor: '#3cb18a',
						borderWidth: route.mode === 'WALKING' ? 0 : 1,
						arrowLine: true,
						dottedLine: route.mode === 'WALKING' ? true : false,
					})
					if (route.vehicle === 'BUS') {
						path.lines.push({
							name: route.title,
							type: 'bus'
						})
						if (!path.geton) {
							path.geton = route.geton.title
						}
						if (!path.getoff) {
							path.getoff = route.getoff.title
						}
						path.stationCount += Number(route.station_count)
						path.fare += Number(route.price) / 100
					}
					if (route.mode === "WALKING") {
						path.walking += Number(route.distance)
						console.log('WALKING', path.walking)
					}
					path.distance += Number(route.distance)
				} else if (this.trafficType === 'subway') {
					const name = route.title ? (route.title as string).split('号线')[0] + '号线' : ''
					let color = '#000'
					if (route.mode === 'WALKING') {
						color = '#999'
					} else if (route.vehicle === 'BUS') {
						color = '#4fbe99'
					} else if (route.vehicle === 'SUBWAY') {
						color = SUBWAYCOLOR.get(name)
					} else if (route.vehicle === 'RAIL') {
						color = '#48557F'
					}

					arr.push({
						points: pl,
						color,
						width: 6,
						arrowLine: true,
						dottedLine: route.mode === 'WALKING' ? true : false,
					})
					if (route.vehicle === 'RAIL') {
						console.log('vehicle', route.mode, route.vehicle, color, pl)
					}


					if (route.vehicle === 'SUBWAY') {
						path.lines.push({
							name: route.title,
							type: 'subway',
							color
						})
						if (!path.geton) {
							path.geton = route.geton.title
						}
						if (!path.getoff) {
							path.getoff = route.getoff.title
						}
						path.stationCount += Number(route.station_count)
						path.fare += Number(route.price) / 100
					} else if (route.vehicle === 'BUS') {
						path.lines.push({
							name: route.title,
							type: 'bus'
						})
						if (!path.geton) {
							path.geton = route.geton.title
						}
						if (!path.getoff) {
							path.getoff = route.getoff.title
						}
						path.stationCount += Number(route.station_count)
						path.fare += Number(route.price) / 100
					} else if (route.vehicle === 'RAIL') {
						path.lines.push({
							name: route.title,
							type: 'rail'
						})
						if (!path.geton) {
							path.geton = route.geton.title
						}
						if (!path.getoff) {
							path.getoff = route.getoff.title
						}
						path.stationCount += Number(route.station_count)
						path.fare += Number(route.price) / 100
					}
					if (route.mode === "WALKING") {
						path.walking += Number(route.distance)
					}
					path.distance += Number(route.distance)
				}
			})
			this.pathLine.push(path)
			this.result.push(arr)
		})
	}
	resetData() {
		this.defaultArray.length = 0
		this.polylineResult.length = 0
		this.setp.length = 0
		this.result.length = 0
	}
	walkData(routes:any[]){		
		const polylineArray :any[]=[]
		const arr: any[] = []
		routes.forEach((line: any) => {
			var route = line
			var coors: any[] = [], pl: any[] = [];
			coors = route.polyline
			var kr = 10000;
			for (var i = 2; i < coors.length; i++) {
				coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
			}
			//将解压后的坐标放入点串数组pl中
			for (var i = 0; i < coors.length; i += 2) {
				pl.push({ latitude: coors[i], longitude: coors[i + 1] })
			}
			arr.push({
				points: pl,
				color: '#999',
				width: 6,
				borderColor: '#3cb18a',
				borderWidth: 0,
				arrowLine: true,
				dottedLine: true,
			})
			polylineArray.push(arr)
		})
		return [polylineArray]
	}
	init(steps: any[]) {
		// this.resetData()
		this.filterData(steps)
		this.setPolylineExchange(this.defaultArray, 0)

		this.analysis()

		return {
			result: this.result,
			path: this.pathLine
		}
	}
}